<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class HomesController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Homes';

/**
 * Default helper
 *
 * @var array
 */
	
	public $helpers = array('Html', 'Session','Common');

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Service','Error','Tattoo','Skincare','StaticPage');
	
	public $components=array('Email');

/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 */
	public function index() {
		$this->layout='default';		
		
		$tattoos=$this->Tattoo->find('all',array('conditions'=>array('Tattoo.published'=>1,'Tattoo.home'=>1),'order'=>'Tattoo.view_order desc, Tattoo.id desc','limit'=>3));
		$services=$this->Service->find('all',array('conditions'=>array('Service.published'=>1,'Service.home'=>1),'order'=>'Service.view_order desc, Service.id desc'));
		$skincares=$this->Skincare->find('all',array('conditions'=>array('Skincare.published'=>1,'Skincare.home'=>1),'order'=>'Skincare.view_order desc, Skincare.id desc','limit'=>1));
		//t($products);
		//$this->redirect("homes/home");
		$this->set('services',$services);
		$this->set('tattoos',$tattoos);
		$this->set('skincares',$skincares);		
		$this->set('title_for_layout',$this->webConfig['site_name']);
		
		$this->set('metakeyword','day xam, dao tao xam, day xam tham my, phun xam tham my, dạy xăm, đào tạo xăm, dạy xăm thẩm mỹ, phun xăm thẩm mỹ, hoc phun xam tham my');
		$this->set('metadescription','Chuyên ngành đào tạo: Phun xăm thẩm mỹ, Phun thêu điêu khắc 3D, Xăm body, Chăm sóc da... Tư vấn: 094 7708 881 Ms.Thảo, Địa chỉ: 243/12 Nguyễn Kim, P7, Q10, HCM');
		$this->set('ogTitle',$this->webConfig['site_name']);
		$this->set('ogType','article');
		$this->set('ogDescription','day xam, dao tao xam, day xam tham my, phun xam tham my');
		$this->set('ogUrl',SITE_URL);
		$this->set('ogImage',$this->webroot.'/img/webpagethumbnail.jpg');			
		
		
	}
	public function contact(){
		$this->layout='default';		
		$this->set('title_for_layout','Đôi nét về đào tạo Huy Thảo');
		$this->set('css','contact');
		$rs='';
		
		if(!empty($this->request->data) ){
			if(!empty($this->request->data['name']) && !empty($this->request->data['email']) && !empty($this->request->data['content'])){
				//if($this->data['Home']['sec']==$this->Session->read('Captcha')){
				if(!$this->Session->check('lastSendMail') ||  (time()- $this->Session->read('lastSendMail'))>$time){
					//Bat dau xu ly viec goi email
					
					$email_content = "Thông tin người gửi:<br /><br />";			
					$email_content .= "Người gửi: " .$this->request->data['name']."<br />";
					$email_content .= "Email: " .$this->request->data['email']."<br />";
					$email_content .= "Số điện thoại: " .$this->request->data['phone']."<br />";
					$email_content .= "Nội dung: " .$this->request->data['content']."<br />";
					
					
					$email = new CakeEmail();
					$email->from(array($this->request->data['email'] => $this->request->data['name']));
					$email->subject($this->request->data['title']);				
					$email->emailFormat('html');
					$emails= explode(',',$this->webConfig['emailContact']);
					if(count($emails)>1){
						foreach($emails as $k=>$v){
							$email->to($v);
							$result = $email->send($email_content);	
						}				
					}
					else{
						$email->to($this->webConfig['emailContact']);
						$result = $email->send($email_content);
					}				
					
					if($result){
						$rs= "<script>alert('"._resource('Cám ơn đã gửi mail liên hệ!',true)."')</script>";
						$this->data=null;
					}
					else{
						$rs="<script>alert('"._resource('Send email error!',true)."')</script>";
					}
					$this->Session->write('lastSendMail',time());
				}else{
					$rs="<script>alert('"._resource('Bạn đã gửi mail liên hệ rồi, hãy đợi '.$time.' giây rồi gửi tiếp.',true)."')</script>";
				}
			}else{
				$rs="<script>alert('"._resource('Thông tin liên hệ không đầy đủ.',true)."')</script>";
			}
			
		}	
		$this->set('data',$this->request->data);
		
		$contact=null;
		$this->StaticPage->locale=$this->Session->read('siteLang');		
		$contact=$this->StaticPage->findByKeycode('contact');
		$this->set('content',$contact['StaticPage']['content']);		
		$this->set('result',$rs);
		$this->render('contact');		
		
	}
	
public function video(){
		$this->layout='default';		
		
		
		$this->StaticPage->locale=$this->Session->read('siteLang');		
		$contact=$this->StaticPage->findByKeycode('video');
		$this->set('content',$contact['StaticPage']['content']);
				
		$title='video huong dan xam tham my';
		$desc='video huong dan xam tham my, video hướng dẫn xăm thẩm mỹ';
		$this->set('title_for_layout',$title);
		$this->set('metakeyword',$title);
		$this->set('metadescription',$desc);
		$this->set('ogTitle',$title);
		$this->set('ogType','article');
		$this->set('ogDescription',$desc);
		$this->set('ogUrl','/home/video/'.$locale);
		
		$this->render('video');		
		
	}
	
	function admin_index(){	
		$this->layout='admin_default';	
		if(empty($this->user['id'])){
			$this->layout=null;
			$this->render('admin_index1');
		}
		else $this->render('admin_index');
	}
	public function admin_home(){
		$this->render('admin_home');
	}
}
